package com.example.wengwyue.patientapplication.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.Random;
import java.util.regex.Pattern;

/**
 * @author dapeng
 */
public class Utils {

    public final static String D = ",";
    public static final String SYMBOL_EMPTY = "";
    /**
     * 没有网络
     */
    public static final int NETWORKTYPE_INVALID = 0;
    /**
     * wap
     */
    public static final int NETWORKTYPE_WAP = 1;

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /*
     * public static <T extends AbstractAQuery<T>> void loadImageEx(final T aq, final Context context, String big_url,
     * final int presetID) { if (aq != null) { MyLog.error(Utils.class, "Load Image for small:" + big_url);
     * aq.image(big_url, true, true, 0, presetID, new BitmapAjaxCallback() {
     * 
     * @SuppressLint("NewApi")
     * 
     * @Override protected void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
     * 
     * if (bm != null) { if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1 &&
     * bm.getByteCount() < 1000) || (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB_MR1 &&
     * bm.getRowBytes() * bm.getHeight() < 1000)) { MyLog.error(Utils.class, "Load big Image failed"); } else {
     * super.callback(url, iv, bm, status); } } else { MyLog.error(Utils.class, "Load big Image failed"); } } }); } }
     */
    /**
     * 2G网络
     */
    public static final int NETWORKTYPE_2G = 2;
    /**
     * 3G和3G以上网络，或统称为快速网络
     */
    public static final int NETWORKTYPE_3G = 3;
    /**
     * wifi网络
     */
    public static final int NETWORKTYPE_WIFI = 4;
    private static final Pattern PATTERN = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
    public static String NETWORT_2G = "2G";
    public static String NETWORT_3G = "3G";
    public static String NETWORT_4G = "4G";
    public static String NETWORT_WIFI = "WIFI";
    public static char[]
            letterArray =
            {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                    'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                    'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
            };


    /**
     * 对象不为空
     */
    public static boolean notNull(Object obj) {
        if (null != obj && obj != "") {
            return true;
        }
        return false;
    }

    public static boolean isNull(Object obj) {
        if (null == obj || obj == "" || obj.equals("")) {
            return true;
        }
        return false;
    }

    public static int getScreenWidth(Context con) {
        return con.getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight(Context con) {
        return con.getResources().getDisplayMetrics().heightPixels;
    }

    /**
     * 给图片补全地址
     */
    public static String getImageUrl(String url) {
        if (null == url) {
            return null;
        }
        if (isURL(url)) {
            return url;
        }
        return url;
    }

    /**
     * 判断字符串是否为URL
     */
    public static boolean isURL(String url) {
        if (!TextUtils.isEmpty(url) && PATTERN.matcher(url).find()) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 获取网络状态，wifi,wap,2g,3g.
     *
     * @param context 上下文
     * @return int 网络状态 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}, {@link
     * #NETWORKTYPE_INVALID}, {@link #NETWORKTYPE_WAP}* <p> {@link #NETWORKTYPE_WIFI}
     */

    public static int getNetWork(Context context) {
        int mNetWorkType = NETWORKTYPE_WIFI;
        ConnectivityManager
                manager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();

            if (type.equalsIgnoreCase("WIFI")) {
                mNetWorkType = NETWORKTYPE_WIFI;
            } else if (type.equalsIgnoreCase("MOBILE")) {
                String proxyHost = android.net.Proxy.getDefaultHost();

                mNetWorkType =
                        TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context)
                                ? NETWORKTYPE_3G : NETWORKTYPE_2G)
                                : NETWORKTYPE_WAP;
            }
        } else {
            mNetWorkType = NETWORKTYPE_INVALID;
        }

        return mNetWorkType;
    }

    public static String getNetWorkTypeDescription(Context context) {
        String mNetWorkType = NETWORT_WIFI;
        ConnectivityManager
                manager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            String type = networkInfo.getTypeName();
            if (type.equalsIgnoreCase(NETWORT_WIFI)) {

                mNetWorkType = networkInfo.getTypeName();
            } else if (type.equalsIgnoreCase("MOBILE")) {
                String proxyHost = android.net.Proxy.getDefaultHost();

                mNetWorkType =
                        TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORT_3G
                                : NETWORT_2G)
                                : NETWORT_2G;
            }
        }
        return mNetWorkType;
    }

    private static boolean isFastMobileNetwork(Context context) {
        TelephonyManager
                telephonyManager =
                (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        switch (telephonyManager.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_1xRTT:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_CDMA:
                return false; // ~ 14-64 kbps
            case TelephonyManager.NETWORK_TYPE_EDGE:
                return false; // ~ 50-100 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
                return true; // ~ 400-1000 kbps
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
                return true; // ~ 600-1400 kbps
            case TelephonyManager.NETWORK_TYPE_GPRS:
                return false; // ~ 100 kbps
            case TelephonyManager.NETWORK_TYPE_HSDPA:
                return true; // ~ 2-14 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPA:
                return true; // ~ 700-1700 kbps
            case TelephonyManager.NETWORK_TYPE_HSUPA:
                return true; // ~ 1-23 Mbps
            case TelephonyManager.NETWORK_TYPE_UMTS:
                return true; // ~ 400-7000 kbps
            case TelephonyManager.NETWORK_TYPE_EHRPD:
                return true; // ~ 1-2 Mbps
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
                return true; // ~ 5 Mbps
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return true; // ~ 10-20 Mbps
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return false; // ~25 kbps
            case TelephonyManager.NETWORK_TYPE_LTE:
                return true; // ~ 10+ Mbps
            case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                return false;
            default:
                return false;
        }
    }

    // 判断网络类型
    public static boolean isWap() {
        String proxyHost = android.net.Proxy.getDefaultHost();
        if (proxyHost != null) {
            return true;
        } else {
            return false;
        }
    }

    public static String getRandomLetters(int n) {
        Random rd = new Random();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < n; i++)// 随即10个拿出来看看
        {
            buffer.append(letterArray[rd.nextInt(52)]);
        }

        return buffer.toString();
    }

//    // password转码 1.App调用中间层接口时，请对password进行加密，加密规则为在3/5/7/9位均插入一数字或字母,加上时间戳后做base64的加密。
//    public static String getParamPassword(String password) {
//        String result = null;
//        if (password != null) {
//
//            int i = 0;
//
//            // 再加时间戳
//            password += System.currentTimeMillis() / 1000;
//
//            // 在第2位插入随机值
//            if (password.length() >= 3) {
//                result = password.substring(0, 2);
//                result += Utils.getRandomLetters(1);
//                i = 2;
//            }
//
//            // 在第4位插入随机值
//            if (password.length() >= 5) {
//                result += password.substring(2, 4);
//                result += Utils.getRandomLetters(1);
//                i = 4;
//            }
//
//            // 在第6位插入随机值
//            if (password.length() >= 7) {
//                result += password.substring(4, 6);
//                result += Utils.getRandomLetters(1);
//                i = 6;
//            }
//
//            // 在第8位插入随机值
//            if (password.length() >= 9) {
//                result += password.substring(6, 8);
//                result += Utils.getRandomLetters(1);
//                i = 8;
//            }
//
//            result += password.substring(i);
//        }
//
//        // 再对result进行base64转码
//        if (result != null) {
//            // data = android.util.Base64.encode(data.getBytes(), android.util.Base64.DEFAULT).toString();
//            result = Base64.encodeBytes(result.getBytes());
//        }
//
//        return result;
//    }

    /**
     * 判断网络是否畅通
     */
    public static boolean isNetworkAvailable(Context context) {

        if (context == null) {
            return false;
        }

        ConnectivityManager
                connectivity =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        } else {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                int l = info.length;
                for (int i = 0; i < l; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }






}
